package com.helper.dream.a361phonehelper.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.helper.dream.a361phonehelper.R;
import com.helper.dream.a361phonehelper.bean.TuijianBannerEntity;
import com.helper.dream.a361phonehelper.inter.ITuijianBannerInterface;
import com.helper.dream.a361phonehelper.utils.ApiManager;
import com.helper.dream.a361phonehelper.utils.HttpClientUtil;
import com.helper.dream.a361phonehelper.utils.L;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by LamYu on 16/12/19/0019.
 * 推荐
 */

public class TuijianFragment extends Fragment {
    View view;
     List<String> tabTitles=new ArrayList<>();
    List<Fragment> fragments;
    TabLayout tab;
    ViewPager vp;
    FragmentPagerAdapter adapter;
    Banner banner;
    List<String> images=new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private void getData() {
        OkHttpClient client = HttpClientUtil.initClient();
        Retrofit retrofit=new Retrofit.Builder().baseUrl(ApiManager.TjBannerBase)
                .client(client).addConverterFactory(GsonConverterFactory.create()).build();
        ITuijianBannerInterface iTuijianBannerInterface=retrofit.create(ITuijianBannerInterface.class);
        Call<TuijianBannerEntity> call=iTuijianBannerInterface.getTuijianBanner();
        call.enqueue(new Callback<TuijianBannerEntity>() {
            @Override
            public void onResponse(Call<TuijianBannerEntity> call, Response<TuijianBannerEntity> response) {
                TuijianBannerEntity body = response.body();
                for (int i = 0; i < body.getData().getBanners().size(); i++) {
                    String url = body.getData().getBanners().get(i).getImage_url_704_244();
                    images.add(url);
                }
                for (int i = 0; i < body.getData().getTabs().size(); i++) {
                    tabTitles.add(body.getData().getTabs().get(i).getTitle());
                }
                adapter=new FragmentPagerAdapter(getChildFragmentManager()) {
                    @Override
                    public Fragment getItem(int position) {
                        return fragments.get(position);
                    }

                    @Override
                    public int getCount() {
                        return fragments.size();
                    }

                    @Override
                    public CharSequence getPageTitle(int position) {
                        return tabTitles.get(position);
                    }
                };
                vp.setAdapter(adapter);
                tab.setupWithViewPager(vp);
                banner= (Banner) view.findViewById(R.id.banner_tj);
                banner.setImages(images);
                banner.setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        Glide.with(getActivity()).load(path).into(imageView);
                    }
                });
                banner.startAutoPlay();
                banner.start();
            }

            @Override
            public void onFailure(Call<TuijianBannerEntity> call, Throwable t) {

            }
        });
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_tuijian, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupViews();
    }

    private void setupViews() {
        getData();

        tab= (TabLayout) view.findViewById(R.id.table_tj);
        vp= (ViewPager) view.findViewById(R.id.vp_tj);
        fragments=new ArrayList<>();
        TuijianFenleiFragment fenleiFragment=new TuijianFenleiFragment();
        fragments.add(fenleiFragment);
        TuijianJingpinFragment jingpinFragment=new TuijianJingpinFragment();
        fragments.add(jingpinFragment);
        TuijianZhiboFragment zhiboFragment=new TuijianZhiboFragment();
        fragments.add(zhiboFragment);
        TuiJianFuliFragment fuliFragment=new TuiJianFuliFragment();
        fragments.add(fuliFragment);
        TuijianBibeiFragment bibeiFragment=new TuijianBibeiFragment();
        fragments.add(bibeiFragment);
        TuijianYuleFragment yuleFragment=new TuijianYuleFragment();
        fragments.add(yuleFragment);

    }
}
